package com.neusoft.util; 

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
* 读取properties文件 
* @author 郑兴龙 zheng-xl@neusoft.com
* 
*/ 
public class Configuration{ 
	//日志对象
	Log log = LogFactory.getLog(Configuration.class);
    private Properties propertie; 
    private FileInputStream inputFile; 
    private static FileOutputStream outputFile; 
    private static Configuration instance = new Configuration();
    public static Configuration getInstance() {
        return instance;
    }

    /**
     * 初始化Configuration类 
     */ 
    public Configuration(){ 
        propertie = new Properties(); 
    } 

    /**
     * 初始化Configuration类 
     * @param filePath 要读取的配置文件的路径+名称 
     */ 
    public Configuration(String filePath){ 
        propertie = new Properties(); 
        try { 
            inputFile = new FileInputStream(filePath); 
            propertie.load(inputFile); 
            inputFile.close(); 
        } catch (FileNotFoundException ex) { 
        	log.error("读取属性文件--->失败！- 原因：文件路径错误或者文件不存在");
            ex.printStackTrace(); 
        } catch (IOException ex) { 
        	log.error("装载文件--->失败!");
            ex.printStackTrace(); 
        } 
    }

    /**
     * 重载函数，得到key的值 
     * @param key 取得其值的键 
     * @return key的值 
     */ 
    public String getValue(String key){ 
        if(propertie.containsKey(key)){ 
            String value = propertie.getProperty(key);//得到某一属性的值 
            return value; 
        } 
        else 
            return ""; 
    }

    /**
     * 重载函数，得到key的值 
     * @param fileName properties文件的路径+文件名 
     * @param key 取得其值的键 
     * @return key的值 
     */ 
    public String getValue(String fileName, String key) { 
        try { 
            String value = ""; 
            inputFile = new FileInputStream(fileName); 
            propertie.load(inputFile); 
            inputFile.close(); 
            if(propertie.containsKey(key)){ 
                value = propertie.getProperty(key); 
                return value; 
            }else 
                return value; 
        }catch (Exception ex) { 
            ex.printStackTrace(); 
        } 
        return ""; 
    }
    
    /**
     * 读取properties的全部信息
     */
	public static  Map<String,String> readProperties(String filePath) {
		Properties props = new Properties();
		Map<String,String>_temp = new HashMap<String, String>();
		try {
			InputStream in = new BufferedInputStream(new FileInputStream(filePath));
			props.load(in);
			Enumeration en = props.propertyNames();
			while (en.hasMoreElements()) {
				String key = (String) en.nextElement();
				String Property = props.getProperty(key);
				//System.out.println(key + Property);
				_temp.put(key, Property);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return _temp;
	}
	
	/**
	 * 写入properties信息
	 */
	public static  void writeProperties(String filePath,String parameterName,String parameterValue) {
		Properties prop = new Properties();
		try {
			InputStream fis = new FileInputStream(filePath);
			// 从输入流中读取属性列表
			prop.load(fis);
			// 表中的属性列表,写入输出流
			outputFile = new FileOutputStream(filePath);
			prop.setProperty(parameterName, parameterValue);
			prop.store(outputFile, "Update '" + parameterName + "' value");
			outputFile.close(); 
		} catch (IOException e) {
			e.getStackTrace();
			//System.err.println("Visit " + filePath + " for updating " + parameterName + " value error");
		}
	}
    /**
     * 清除properties文件中所有的key和其值 
     */ 
    public void clear(){ 
        propertie.clear(); 
    }
    /** 
     * 改变或添加一个key的值，当key存在于properties文件中时该key的值被value所代替， 
     * 当key不存在时，该key的值是value 
     * @param key 要存入的键 
     * @param value 要存入的值 
     */ 
    public void setValue(String key, String value) { 
        propertie.setProperty(key, value); 
    }
}